#include <iostream>
#include <vector>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include<netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
using namespace std;
#define BUFSIZE 1024
int main(int argc, char*argv[])
{
    int on = 1;
    int backlog = 10;
    int socketId = socket(AF_INET, SOCK_STREAM, 0);
    printf("server listening:%d\n", socketId);
    if (socketId < 0) {
        cerr << "Error creating socket" << endl;
        return -1;
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(33043);
    addr.sin_addr.s_addr = inet_addr("192.168.203.133");
    int ret = connect(socketId, (struct sockaddr *)&addr, sizeof(addr));
    printf("err number ret:%d\n", errno);
    if (ret < 0) {
        cerr << "Error connecting to server" << endl;
        return -1;
    }

    char buf[BUFSIZE];
    char sendbuf[BUFSIZE];
    while(1) {
        memset(sendbuf, 0, sizeof(sendbuf));
        cout << "Enter message: ";
        cin.getline(sendbuf, BUFSIZE);
        send(socketId,sendbuf,BUFSIZE, 0);
        
        if (strcasecmp(sendbuf, "quit") == 0) {
            cout << "quit" << endl;
            break;
        }
        int retLen = recv(socketId,buf,BUFSIZE,0);
        buf[retLen] = '\0';
        if (retLen <= 0) {
            cerr << "Error receiving data" << endl;
            break;
        }

        cout << "Server: " << buf << endl;
    }

    close(socketId);
    return 0;
}